package com.twd.redis.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

@Configuration
public class RedisConfig {

    //自定义序列化固定模板，直接使用即可
    @Bean("RedisTemplate")
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        //为了开发方便用<String, Object>
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
//        //序列化配置
//        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//        ObjectMapper om = new ObjectMapper();
//        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//        jackson2JsonRedisSerializer.setObjectMapper(om);
//        //String序列化
//        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//        //KEY采用string的序列化方式
//        template.setKeySerializer(stringRedisSerializer);
//        //hash
//        template.setHashKeySerializer(jackson2JsonRedisSerializer);
//        //value序列化
//        template.setValueSerializer(jackson2JsonRedisSerializer);
//        template.afterPropertiesSet();
        RedisSerializer stringSerializer = new StringRedisSerializer();//序列化为String
        //不能反序列化
        //Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);//序列化为Json
        GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(serializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(serializer);
        return redisTemplate;
    }

    static  class Test implements Callable<String>{

        String a = "";
        @Override
        public String call() throws Exception {
            return a;
        }

        public void setA(String a){
            this.a = a;
        }
    }
    static  class FutureTest extends FutureTask<String>{
        String a = "";
        Test t ;
        public FutureTest(Test callable) {
            super(callable);
            t = callable;
        }
        public void setA(String a) {
            t.setA(a);
        }
    }
    public static void main(String[] args) {
//        Test c = new Test();
//        FutureTest f = new FutureTest(c);
//        new Thread(()->{
//            try {
//                TimeUnit.SECONDS.sleep(5);
//            } catch (InterruptedException e) {
//            }
//            f.setA("wwwwwwwww");
//            f.run();
//        }).start();
//
//        new Thread(()-> {
//            try {
//                System.out.println(f.get());
//            } catch (InterruptedException e) {
//
//
//            } catch (ExecutionException e) {
//                e.printStackTrace();
//            }
//        }).start();
        System.out.println(get(null));
    }

    public static String get(String a){
        throw new NullPointerException();
    }
}